<?php
namespace app\demo\model;
use traits\model\SoftDelete;
use app\mogosdk\model\Mogo;

use EasyWeChat\Factory;
class User extends Mogo
{
    use SoftDelete;
    protected $table = 'dp_xdk_user';
    protected $autoWriteTimestamp = true;

    protected function getAvatarAttr($value){
        return get_file_path($value);
    }

    /**
     * 注册
     * @return \think\response\Json
     */
    public function register(){

        $data = request()->only(['username','mobile','avatar']);
        $validate = new \app\xdk\validate\User();
        $result =  $validate->scene('register')->check($data);
        if(!$result){
           return  json(['error'=>$validate->getError()],422);
        }
        $result  =$this->create($data);
        return json($result);
    }

    public function requestSmsCode(){
        $data = request()->only(['mobile']);
        $data['code'] = 123456;
        return json($data);
    }

    /**
     * 登录
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function login(){

        // 这里的登陆逻辑自己实现
        $data = request()->only(['mobile']);
        $user = User::get($data);
//        echo json_encode($user);die;
        $jwt = $this->userLogin($user);
        $user->token = $jwt;

        return json($user);
    }

    /**
     * 检测用户状态
     * @return object
     */
    public function userStatus(){

        return $this->authority();
    }

    
}
